import java.util.ArrayList;
import java.util.List;

class Solution {
    public List<String> findMissingRanges(int[] nums, int lower, int upper) {
        int n = nums.length;
        List<String> res = new ArrayList<>();

        if (n==0) {
            res.add(help(lower-1, upper+1));
            return res;
        }
        if (lower < nums[0]) {
            res.add(help(lower - 1, nums[0]));
        }
        for (int i=1; i<n; i++) {
            if (nums[i] != nums[i-1] + 1) {
                res.add(help(nums[i-1], nums[i]));
            }
        }
        if (upper > nums[n-1]) {
            res.add(help(nums[n-1], upper+1));
        }
        return res;
    }

    String help(int left, int right) {
        StringBuilder sb = new StringBuilder();
        sb.append(left + 1);
        if (right - left != 2) {
            sb.append('-').append('>').append(right - 1);
        }
        return sb.toString();
    }

}